/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view;

import com.ywb.edugui.EduguiApplication;
import com.ywb.edugui.view.subject.SubjectAddPanel;
import com.ywb.edugui.view.subject.SubjectListPanel;
import com.ywb.edugui.view.teacher.TeacherAddOrUpdatePanel;
import com.ywb.edugui.view.teacher.TeacherListPanel;
import com.ywb.edugui.view.user.LoginView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.event.MouseEvent;

/**
 *
 * @author 10147
 */
public class MainView extends javax.swing.JFrame {
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JButton jbExit;
    private javax.swing.JButton jbSubjectAdd;
    private javax.swing.JButton jbSubjectList;
    private javax.swing.JButton jbTeacherAdd;
    private javax.swing.JButton jbTeacherList;
    private javax.swing.JLabel jlChinese;
    private javax.swing.JLabel jlEnligsh;
    private javax.swing.JLabel jlSpiliter;
    private javax.swing.JLabel jlSubjectManage;
    private javax.swing.JLabel jlTeacherManage;
    private javax.swing.JLabel jlTitle;
    private static javax.swing.JTabbedPane jpChildren;
    private javax.swing.JPanel jpLeftMenu;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem miClose;
    private javax.swing.JMenuItem miCloseAll;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration

    private static Locale locale = Locale.getDefault();
    private static ResourceBundle bundle = ResourceBundle.getBundle("i18n/resource",locale);
    private MenuItem miExit = null;
    private MenuItem miShow = null;
    private MenuItem miTeacherAdd = null;
    private MenuItem miTeacherList = null;
    private MenuItem miSubjectAdd = null;
    private MenuItem miSubjectList = null;
    private TrayIcon trayIcon = null;
    /**
     * Creates new form MainForm
     */
    public MainView() {
        initComponents();
        setLocationRelativeTo(null);
        jpLeftMenu.setOpaque(false);
        jpChildren.setOpaque(false);
        mainPanel.setBackground(Color.WHITE);
        setTimerLabel();
        addSystemTray();
        setRegionText();
    }


    private void setTimerLabel() {
        new Thread(() -> {
            while (true) {
                //获取系统当前时间
                LocalDateTime now = LocalDateTime.now();
                //格式化时间
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
                String timeString = dateTimeFormatter.format(now);
                //启一个UI线程
                EventQueue.invokeLater(() -> {
                    //将格式化后的日期时间显示在标签上
                    timeLabel.setText(timeString);
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.err.println("中断异常");
                }
            }
        }).start();
    }

    /**
     * 添加系统托盘
     */
    private void addSystemTray() {
        if(SystemTray.isSupported() == false){
            return;
        }
//          加载图标
        URL url = EduguiApplication.class.getResource("/images/tray.png");// 获取图片所在的 URL
        trayIcon = new TrayIcon(new ImageIcon(url).getImage());
//            双击托盘正常显示或最小化
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    showOrHide();
                }
            }
        });

        trayIcon.setToolTip(bundle.getString("main.systemname"));
//        托盘弹出菜单：
//        需要在configuration中add vm options:-Dfile.encoding=gbk 否则托盘字体乱码
        PopupMenu popupMenu = new PopupMenu();

        miShow = new MenuItem(bundle.getString("main.showorhide"));
        miShow.addActionListener(e -> showOrHide());
        popupMenu.add(miShow);

        popupMenu.addSeparator();
        miTeacherAdd = new MenuItem(bundle.getString("teacher.add"));
        miTeacherAdd.addActionListener(e -> showFrame(jbTeacherAdd::doClick));
        popupMenu.add(miTeacherAdd);
        miTeacherList = new MenuItem(bundle.getString("teacher.list"));
        miTeacherList.addActionListener(e -> showFrame(jbTeacherList::doClick));
        popupMenu.add(miTeacherList);

        miSubjectAdd = new MenuItem(bundle.getString("subject.add"));
        miSubjectAdd.addActionListener(e -> showFrame(jbSubjectAdd::doClick));
        popupMenu.add(miSubjectAdd);

        miSubjectList = new MenuItem(bundle.getString("subject.list"));
        miSubjectList.addActionListener(e -> showFrame(jbSubjectList::doClick));
        popupMenu.add(miSubjectList);

        popupMenu.addSeparator();
        miExit = new MenuItem(bundle.getString("main.exit"));
        miExit.addActionListener(e -> System.exit(0));
        popupMenu.add(miExit);

        trayIcon.setPopupMenu(popupMenu);
//            获得系统托盘并添加
        SystemTray systemTray = SystemTray.getSystemTray();
        try {
            systemTray.add(trayIcon);
        } catch (Exception e) {
            System.out.println("当前系统不支持托盘");
            e.printStackTrace();
        }
    }
    private void showFrame(){
        setVisible(true);
        setState(Frame.NORMAL);
    }
    private void showFrame(boolean hide){
        if(hide == true)
            setState(Frame.ICONIFIED);
        else {
            showFrame();
        }
    }
    private void showOrHide(){
        if(getState() == Frame.ICONIFIED){
            showFrame();
        }else{
            showFrame(true);
        }
    }

    /**
     * @param action Lambda表达式，接收一个将会执行的代码片段
     */
    private void showFrame(Runnable action){
        showFrame();
        action.run();
    }

    /**
     * 将页面上的label和button设置为和地区对应的文字
     */
    private void setRegionText() {
        bundle = ResourceBundle.getBundle("i18n/resource",locale);
        jlTeacherManage.setText(bundle.getString("main.teachermanage"));
        jbTeacherAdd.setText(bundle.getString("main.teacheradd"));
        jbTeacherList.setText(bundle.getString("main.teacherlist"));
        jlSubjectManage.setText(bundle.getString("main.subjectmanage"));
        jbSubjectAdd.setText(bundle.getString("main.subjectadd"));
        jbSubjectList.setText(bundle.getString("main.subjectlist"));
        jbExit.setText(bundle.getString("main.exit"));
        miClose.setText(bundle.getString("main.close"));
        miCloseAll.setText(bundle.getString("main.closeall"));
        miSubjectAdd.setLabel(bundle.getString("main.subjectadd"));
        miSubjectList.setLabel(bundle.getString("main.subjectlist"));
        miTeacherAdd.setLabel(bundle.getString("main.teacheradd"));
        miTeacherList.setLabel(bundle.getString("main.teacherlist"));
        miShow.setLabel(bundle.getString("main.showorhide"));
        miExit.setLabel(bundle.getString("main.exit"));
        trayIcon.setToolTip(bundle.getString("main.systemname"));

        /**
         * 获取tabbedPanel上的所有面板,依次设置国际化
         */
        Component[] panels = jpChildren.getComponents();
        for (int i = 0; i < panels.length; i++) {
            Component panel = panels[i];
            if(panel instanceof TeacherListPanel){
//                修改tabpanel上的标题
                jpChildren.setTitleAt(i, bundle.getString("teacher.list"));
//                修改面板中的文字
                ((TeacherListPanel) panel).setRegionText(bundle);
            }
            if(panel instanceof TeacherAddOrUpdatePanel){
//               根据panel中的isAddPanel判断是新增面板还是修改面板
                if(((TeacherAddOrUpdatePanel) panel).isAddPanel() == true){
                    jpChildren.setTitleAt(i, bundle.getString("teacher.add"));
                }else{
                    jpChildren.setTitleAt(i, bundle.getString("teacher.update"));
                }
                ((TeacherAddOrUpdatePanel) panel).setRegionText(bundle);
            }
            if(panel instanceof SubjectAddPanel){
                jpChildren.setTitleAt(i, bundle.getString("subject.add"));
                ((SubjectAddPanel)panel).setRegionText(bundle);
            }
            if(panel instanceof SubjectListPanel){
                jpChildren.setTitleAt(i, bundle.getString("subject.list"));
            }
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        miClose = new javax.swing.JMenuItem();
        miCloseAll = new javax.swing.JMenuItem();
        mainPanel = new javax.swing.JPanel();
        jlTitle = new javax.swing.JLabel();
        jpLeftMenu = new javax.swing.JPanel();
        jlTeacherManage = new javax.swing.JLabel();
        jbTeacherList = new javax.swing.JButton();
        jbTeacherAdd = new javax.swing.JButton();
        jlSubjectManage = new javax.swing.JLabel();
        jbSubjectList = new javax.swing.JButton();
        jbSubjectAdd = new javax.swing.JButton();
        jlChinese = new javax.swing.JLabel();
        jlSpiliter = new javax.swing.JLabel();
        jlEnligsh = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        jbExit = new javax.swing.JButton();
        jpChildren = new javax.swing.JTabbedPane();

        jPopupMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPopupMenu1MouseClicked(evt);
            }
        });

        miClose.setText("关闭选项卡");
        miClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miCloseActionPerformed(evt);
            }
        });
        jPopupMenu1.add(miClose);

        miCloseAll.setText("关闭所有选项卡");
        miCloseAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miCloseAllActionPerformed(evt);
            }
        });
        jPopupMenu1.add(miCloseAll);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mainPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mainPanelMouseClicked(evt);
            }
        });

        jlTitle.setIcon(new javax.swing.ImageIcon("D:\\Program Files\\CodeProject\\javaprojects\\edugui\\src\\main\\resources\\images\\logo.png")); // NOI18N
        jlTitle.setText("标题栏图标");

        jpLeftMenu.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jpLeftMenu.setToolTipText("");

        jlTeacherManage.setBackground(new java.awt.Color(204, 255, 255));
        jlTeacherManage.setFont(new java.awt.Font("黑体", 0, 14)); // NOI18N
        jlTeacherManage.setText("教师管理");

        jbTeacherList.setText("教师列表");
        jbTeacherList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTeacherListActionPerformed(evt);
            }
        });

        jbTeacherAdd.setText("教师添加");
        jbTeacherAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTeacherAddActionPerformed(evt);
            }
        });

        jlSubjectManage.setFont(new java.awt.Font("黑体", 0, 14)); // NOI18N
        jlSubjectManage.setText("科目管理");

        jbSubjectList.setText("SubjectList");
        jbSubjectList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSubjectListActionPerformed(evt);
            }
        });

        jbSubjectAdd.setText("SubjectAdd");
        jbSubjectAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSubjectAddActionPerformed(evt);
            }
        });

        jlChinese.setText("中文");
        jlChinese.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlChineseMouseClicked(evt);
            }
        });

        jlSpiliter.setText("|");

        jlEnligsh.setText("英文");
        jlEnligsh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlEnligshMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jpLeftMenuLayout = new javax.swing.GroupLayout(jpLeftMenu);
        jpLeftMenu.setLayout(jpLeftMenuLayout);
        jpLeftMenuLayout.setHorizontalGroup(
                jpLeftMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpLeftMenuLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jlTeacherManage, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jpLeftMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jpLeftMenuLayout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(jbTeacherAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpLeftMenuLayout.createSequentialGroup()
                                        .addGap(19, 19, 19)
                                        .addComponent(jbTeacherList, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jpLeftMenuLayout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addComponent(jlChinese)
                                .addGap(6, 6, 6)
                                .addComponent(jlSpiliter)
                                .addGap(14, 14, 14)
                                .addComponent(jlEnligsh))
                        .addGroup(jpLeftMenuLayout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(jpLeftMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jbSubjectList, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jbSubjectAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jpLeftMenuLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jlSubjectManage, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jpLeftMenuLayout.setVerticalGroup(
                jpLeftMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpLeftMenuLayout.createSequentialGroup()
                                .addGap(46, 46, 46)
                                .addComponent(jlTeacherManage)
                                .addGap(18, 18, 18)
                                .addComponent(jbTeacherList, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jbTeacherAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(44, 44, 44)
                                .addComponent(jlSubjectManage)
                                .addGap(18, 18, 18)
                                .addComponent(jbSubjectList, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)
                                .addComponent(jbSubjectAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 291, Short.MAX_VALUE)
                                .addGroup(jpLeftMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jlChinese)
                                        .addComponent(jlSpiliter)
                                        .addComponent(jlEnligsh))
                                .addGap(19, 19, 19))
        );

        jLabel1.setIcon(new javax.swing.ImageIcon("D:\\Program Files\\CodeProject\\javaprojects\\edugui\\src\\main\\resources\\images\\spiliter.png")); // NOI18N
        jLabel1.setText("jLabel1");
        jLabel1.setPreferredSize(new java.awt.Dimension(2570, 10));

        timeLabel.setBackground(new java.awt.Color(204, 204, 255));
        timeLabel.setFont(new java.awt.Font("幼圆", 0, 14)); // NOI18N
        timeLabel.setForeground(new java.awt.Color(0, 153, 153));
        timeLabel.setText("2001/06/09 20:46:33");

        jbExit.setFont(new java.awt.Font("黑体", 0, 12)); // NOI18N
        jbExit.setText("退出系统");
        jbExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExitActionPerformed(evt);
            }
        });

        jpChildren.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jpChildrenMouseClicked(evt);
            }
        });
        jpChildren.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jpChildrenKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1260, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                                .addComponent(jpLeftMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jpChildren))
                                        .addGroup(mainPanelLayout.createSequentialGroup()
                                                .addGap(27, 27, 27)
                                                .addComponent(jlTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(jbExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE))))
                                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(mainPanelLayout.createSequentialGroup()
                                                .addComponent(jlTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(mainPanelLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(jbExit, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(28, 28, 28)))
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jpLeftMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jpChildren, javax.swing.GroupLayout.PREFERRED_SIZE, 657, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    private void jlEnligshMouseClicked(java.awt.event.MouseEvent evt) {
        locale = Locale.US;
        setRegionText();
    }

    private void jlChineseMouseClicked(java.awt.event.MouseEvent evt) {
        locale = Locale.CHINA;
        setRegionText();
    }

    private void jbSubjectAddActionPerformed(java.awt.event.ActionEvent evt) {
        changeChildrenPanel(bundle.getString("subject.add"),new SubjectAddPanel());
    }

    private void jbSubjectListActionPerformed(java.awt.event.ActionEvent evt) {
        changeChildrenPanel(bundle.getString("subject.list"),new SubjectListPanel());
    }

    private void jbTeacherAddActionPerformed(java.awt.event.ActionEvent evt) {
        changeChildrenPanel( bundle.getString("teacher.add"),new TeacherAddOrUpdatePanel());
    }

    private void jbTeacherListActionPerformed(java.awt.event.ActionEvent evt) {
        changeChildrenPanel(bundle.getString("teacher.list"),new TeacherListPanel());

    }

    private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
        EventQueue.invokeLater(() ->{
            new LoginView().setVisible(true);
            setVisible(false);
        });
    }

    private void jPopupMenu1MouseClicked(java.awt.event.MouseEvent evt) {
    }

    private void miCloseActionPerformed(java.awt.event.ActionEvent evt) {
        jpChildren.remove(jpChildren.getSelectedIndex());
    }

    private void jpChildrenMouseClicked(java.awt.event.MouseEvent evt) {
        showPopupMenu(evt);
    }

    private void mainPanelMouseClicked(java.awt.event.MouseEvent evt) {
        showPopupMenu(evt);

    }

    private void miCloseAllActionPerformed(java.awt.event.ActionEvent evt) {
        jpChildren.removeAll();
    }

    private void jpChildrenKeyReleased(java.awt.event.KeyEvent evt) {
        if(evt.getModifiers() == 2 && evt.getKeyCode() == 18){
            changeChildrenPanel();
        }
    }

    /**
     * 不带panel的调用为切换到当前选项卡的下一个选项卡
     */
    public static void changeChildrenPanel(){
//        切换至下一个panel
        jpChildren.setSelectedIndex((jpChildren.getSelectedIndex() + 1) % jpChildren.getTabCount());
    }

    /**
     * 创建名为title的panel选项卡并获得焦点
     * @param title
     * @param panel
     */
    public static void changeChildrenPanel(String title,JPanel panel){
        /**
         * 遍历当前选项卡组,如果已经有该窗口就直接聚焦，否则新建，并聚焦
         */
        for (int i = 0; i < jpChildren.getTabCount(); i++) {
            if(jpChildren.getTitleAt(i).equals(title)){
                jpChildren.setSelectedIndex(i);
                return;
            }
        }
//        将子panel添加到tabbedpanel中
        panel.setBackground(Color.WHITE);
        jpChildren.add(title ,panel);
//        将新增的panel设置为被选中
        jpChildren.setSelectedIndex(jpChildren.getTabCount() - 1);
        //        获得焦点,用于ctrl + tab切换
        jpChildren.grabFocus();
    }
    /**
     * 创建名为title的panel选项卡并获得焦点
     * @param title
     * @param panel
     */
    public static void changeChildrenPanel(String title,JPanel panel, boolean isDelete){
        if(isDelete == true){
            jpChildren.remove(jpChildren.getSelectedIndex());
        }
        changeChildrenPanel(title, panel);
    }

    private void showPopupMenu(MouseEvent e){
//        判断鼠标点击的是否为右键,如果是就弹出菜单
        if(e.getButton() == MouseEvent.BUTTON3){
//          将弹出菜单在鼠标点击处显示
            jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public static ResourceBundle getBundle(){
        return bundle;
    }
}
